package com.yutel.google;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import cn.com.xinli.android.service.protocol.BoxAuth;

public class TestBoxAuth {
	private String AUTHIPADRESS="192.168.2.243";
	private int connTimeOut=20000;
	private int dataTimeOut=60000;
	public static void main(String[] arg) {
		String user="000053972394";
		String mac="00:13:ef:a0:ca:10";
		String sesseion="0000504190881353202595913";
		boolean phone=false;
		List<String> iid=new ArrayList<String>();
		iid.add("2324");
		TestBoxAuth t = new TestBoxAuth();
		// init
		t.testInitAccountRequest(mac);
		// auth
		sesseion = t.testAuthRequest(user, "123", mac);
		// 收藏与关注
		t.testVPListRequest(user, sesseion, null, null, phone);
		t.testAttentionRequest(user, null, null, phone);
		
	}
	public String testInitAccountRequest(String mac) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput=null;
		try {
    		socketClient=new Socket();
    		socketClient.connect(new InetSocketAddress(AUTHIPADRESS,5202), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput=new DataOutputStream(socketClient.getOutputStream());
			BoxAuth.InitAccountRequest.Builder authBuilder = BoxAuth.InitAccountRequest.newBuilder();
			authBuilder.setMac(mac);
			BoxAuth.InitAccountRequest authRequest = authBuilder.build();
			dataOutput.writeInt(authRequest.getSerializedSize());
			dataOutput.write(authRequest.toByteArray());
			dataOutput.flush();
			InputStream in = socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length = dataInput.readInt();// 获取返回值的长度
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			BoxAuth.InitAccountResponse authResponse = BoxAuth.InitAccountResponse.parseFrom(dataini);
			if (authResponse != null) {
				System.out.println("getResultCode=" +authResponse.getResultCode());
				System.out.println("getResultMessage=" +authResponse.getResultMessage());
				System.out.println("getUserId="+authResponse.getUserId());
				return authResponse.getUserId();
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public String testAuthRequest(String userId,String pwd,String mac) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput=null;
		try {
    		socketClient=new Socket();
    		socketClient.connect(new InetSocketAddress(AUTHIPADRESS,5203), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput=new DataOutputStream(socketClient.getOutputStream());
			BoxAuth.AuthRequest.Builder authBuilder = BoxAuth.AuthRequest.newBuilder();
			authBuilder.setUserId(userId);
			authBuilder.setPassword(pwd);
			authBuilder.setMac(mac);
			BoxAuth.AuthRequest authRequest = authBuilder.build();
			dataOutput.writeInt(authRequest.getSerializedSize());
			dataOutput.write(authRequest.toByteArray());
			dataOutput.flush();
			InputStream in = socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length = dataInput.readInt();// 获取返回值的长度
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			BoxAuth.AuthResponse authResponse = BoxAuth.AuthResponse.parseFrom(dataini);
			if (authResponse != null) {
				System.out.println("getResultCode=" +authResponse.getResultCode());
				System.out.println("getResultMessage=" +authResponse.getResultMessage());
				System.out.println("getSessionId="+authResponse.getUserInfo().getSessionId());
				System.out.println("getGroup="+authResponse.getUserInfo().getGroup());
				List<String> list = authResponse.getUserInfo().getSettings().getItemList();
				System.out.println("settings list:[");
				for(String i:list) {
					System.out.println(i);
				}
				System.out.println("]");
				return authResponse.getUserInfo().getSessionId();
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public void testVPListRequest(String userid,String sessionid,List<String> insertid,List<String> removeid,boolean phone) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput=null;
		BoxAuth.VPListRequest request=null;
		try {
    		socketClient=new Socket();
    		socketClient.connect(new InetSocketAddress(AUTHIPADRESS,5206), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput=new DataOutputStream(socketClient.getOutputStream());
			BoxAuth.VPListRequest.Builder builder=BoxAuth.VPListRequest.newBuilder();
			builder.setUserId(userid);
			if(sessionid!=null) {
				builder.setSessionId(sessionid);
			}
			builder.setLastModification(0);
			if(phone) {
				builder.setType(1);
			}
			BoxAuth.VPListRequest.UpdateOps.Builder updateOpsBuilder = BoxAuth.VPListRequest.UpdateOps
					.newBuilder();
			// 添加ＩＤ
			if(insertid != null)
				for (String id : insertid) {
					updateOpsBuilder.addInsertId(id);
				}
			// 删除ＩＤ
			if(removeid != null)
				for (String id : removeid) {
					updateOpsBuilder.addRemoveId(id);
				}
			BoxAuth.VPListRequest.UpdateOps ops = updateOpsBuilder.build();
			builder.setFavorites(ops);
			request = builder.build();
			dataOutput.writeInt(request.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(request.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			InputStream in = socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length = dataInput.readInt();// 获取返回值的长度
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			BoxAuth.VPListResponse attentionResponse = BoxAuth.VPListResponse
					.parseFrom(dataini);
			if (attentionResponse != null) {
				System.out.println("getResultCode=" +attentionResponse.getResultCode());
				System.out.println("getResultMessage=" +attentionResponse.getResultMessage());
				System.out.println("full.size="+attentionResponse.getList().getFavoritesList().size());
				System.out.println("full="+attentionResponse.getList().getFavoritesList());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void testAttentionRequest(String userid,List<String> insertid,List<String> removeid,boolean phone) {
		Socket socketClient;
		DataOutputStream dataOutput;
		DataInputStream dataInput=null;
		BoxAuth.AttentionRequest request=null;
		try {
    		socketClient=new Socket();
    		socketClient.connect(new InetSocketAddress(AUTHIPADRESS,5210), connTimeOut);
			socketClient.setSoTimeout(dataTimeOut);
			dataOutput=new DataOutputStream(socketClient.getOutputStream());
			BoxAuth.AttentionRequest.Builder builder=BoxAuth.AttentionRequest.newBuilder();
			builder.setUserId(userid);
			builder.setLastModification(0);
			if(phone) {
				builder.setType(1);
			}
			BoxAuth.AttentionRequest.UpdateOps.Builder updateOpsBuilder = BoxAuth.AttentionRequest.UpdateOps
					.newBuilder();
			// 添加ＩＤ
			if(insertid != null)
				for (String id : insertid) {
					updateOpsBuilder.addInsertId(id);
				}
			// 删除ＩＤ
			if(removeid != null)
				for (String id : removeid) {
					updateOpsBuilder.addRemoveId(id);
				}
			BoxAuth.AttentionRequest.UpdateOps ops = updateOpsBuilder.build();
			builder.setAttentions(ops);
			request = builder.build();
			dataOutput.writeInt(request.getSerializedSize());// 传输序列化的字节长度
			dataOutput.write(request.toByteArray());// 将发送内容转换为字节码数组
			dataOutput.flush();// 清空缓存
			InputStream in = socketClient.getInputStream();
			dataInput = new DataInputStream(new BufferedInputStream(in));
			int length = dataInput.readInt();// 获取返回值的长度
			byte dataini[] = new byte[length];// 设置长度
			dataInput.readFully(dataini);
			dataInput.close();
			socketClient.close();
			BoxAuth.AttentionResponse attentionResponse = BoxAuth.AttentionResponse
					.parseFrom(dataini);
			if (attentionResponse != null) {
				System.out.println("getResultCode=" +attentionResponse.getResultCode());
				System.out.println("getResultMessage=" +attentionResponse.getResultMessage());
				System.out.println("full.size="+attentionResponse.getList().getAttentionsList().size());
				System.out.println("full="+attentionResponse.getList().getAttentionsList());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
